import static cylons.Move.Mode.TARGET;
import cylons.Hunter;
import cylons.HuntingSystem;
import cylons.Move;
import cylons.TargetingSystem;
public class AdmiralAdama extends Player {
	
	private boolean isTargeting;
	private TargetingSystem targeting;
	private Hunter hunter;
	
	private boolean[] enemyShipsRemaining = {true,true,true,true,true};
	
	int lastMoveCol, lastMoveRow, huntShots, targetShots;
	
	public AdmiralAdama(int playerNum) {
		super(playerNum);
        targeting = new TargetingSystem();
        int [] shipLengths = {5,4,3,2,3};
		hunter = new Hunter(shipLengths,enemyShipsRemaining);
	}

	@Override
	public boolean addShips() {
		
		final int BoardDimension = 10;
		final int NumDirections = 4;
		
		for (int ship = Ships.SHIP_SUBMARINE; ship <= Ships.SHIP_CARRIER; ship++) {

			boolean success = false;
			
			while (!success) {
				
				Double randX = (Math.random() * BoardDimension);
				Double randY = (Math.random() * BoardDimension);
				Double randDir = (Math.random() * NumDirections);
				
				int squareX = randX.intValue();
				int squareY = randY.intValue();
				int dir = randDir.intValue();
			
				// Using our random square and random starting direction, try to fit the ship in.
				for (int i = 0; i < NumDirections; i++) {

					// putShip does not verify that the ship will not fall off of the board, so verify that first.
					boolean fits = false;
					
					// The length is the type except for the submarine
					int length;
					if (ship == Ships.SHIP_SUBMARINE) {
						length = 3;
					} else {
						length = ship;
					}
					
					switch(dir) {
						case Ships.SHIP_NORTH:
							if ((squareY - length + 1) >= 0) {
								fits = true;
							}
							break;
						case Ships.SHIP_EAST:
							if ((squareX + length) < BoardDimension) {
								fits = true;
							}
							break;
						case Ships.SHIP_SOUTH:
							if ((squareY + length) < BoardDimension) {
								fits = true;
							}
							break;
						case Ships.SHIP_WEST:
							if ((squareX - length + 1) >= 0) {
								fits = true;
							}
							break;
					}
					
					// If the ship fits on the board, attempt to place it, which may fail if another
					// ship already occupies the area.
					if (fits && game.putShip(myShips, ship, (squareY + 1), (squareX + 1), dir)) {
						success = true;
						break;
					}
					
					// Rotate through each direction.
					dir = ((dir + 1) % NumDirections);
				}
			}
		}

        return true;
	}

	@Override
	public void makeMove() {
        Move nextMove = null;
        if (isTargeting) {
            // Until the targeting system relinquishes control continue to targeting the area
			System.out.println("Targeter firing");
			++targetShots;
            nextMove = targeting.makeMove(myPegsBoard, lastMoveRow, lastMoveCol);
        }

        if (nextMove == null) {
            // Hunt until we have a hit
			System.out.println("Hunter firing");
			++huntShots;
            nextMove = hunter.getNextMove(myPegsBoard);
        }

        numMoves++;
		lastMoveCol = nextMove.getColumn();
		lastMoveRow = nextMove.getRow();
		System.out.println(String.format("Firing at %d,%d", lastMoveRow, lastMoveCol));
		if (game.makeMove(hisShips, myMoves, lastMoveRow + 1, lastMoveCol + 1)) {
			System.out.println(String.format("Last move %d,%d=%s. Hunt Shots %d, Target Shots %d", lastMoveRow,
					lastMoveCol, myPegsBoard[lastMoveRow][lastMoveCol], huntShots, targetShots));

            // If we are still in target mode, or our last move was a hit then continue targeting
            isTargeting = nextMove.getMode() == TARGET || myPegsBoard[lastMoveRow][lastMoveCol] == 'H';
        } else {
			while(!game.makeMove(hisShips, myMoves, randomRow(), randomCol()));
			//throw new RuntimeException("oops");
        }
	}
	
	@Override
	public void sankCarrier() {
		System.out.println("You Sank my Carrier(p" + myPlayerNum + ")");
		destroyShip(Ships.SHIP_CARRIER);
	}

	@Override
	public void sankBattleShip() {
		System.out.println("You Sank my Battleship(p" + myPlayerNum + ")");
		destroyShip(Ships.SHIP_BATTLESHIP);
	}

	@Override
	public void sankCruiser() {
		System.out.println("You Sank my Cruiser(p" + myPlayerNum + ")");
		destroyShip(Ships.SHIP_CRUISER);
	}

	@Override
	public void sankDestroyer() {
		System.out.println("You Sank my Destroyer(p" + myPlayerNum + ")");		
		destroyShip(Ships.SHIP_DESTROYER);
	}

	@Override
	public void sankSubmarine() {
		System.out.println("You Sank my Submarine(p" + myPlayerNum + ")");
		destroyShip(Ships.SHIP_SUBMARINE);
	}

	private void destroyShip(int shipIndex) {
		enemyShipsRemaining[shipIndex-1] = false;
	}

}
